package _2Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/setsession")
public class SetSessionServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      //1.得到会话对象
        //用户第一次访问 会创建Session,如果不是第一次，则得到他当前的 会话
        HttpSession session=req.getSession();

        //2存入信息
        session.setAttribute("name","tom");
        session.setAttribute("age","18");

        //3、设定有效时间 单位是秒，默认30分钟
        session.setMaxInactiveInterval(4*60);

        //4.让会话失效 用户登出
        session.invalidate();


        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print("设置会话信息");
    }
}
